<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入物料名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spec">
              <a-input v-model="model.spec" placeholder="请输入规格"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rremark">
              <a-input v-model="model.rremark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input v-model="model.unit" placeholder="请输入单位"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="解析规则" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="analysis">
              <!-- <a-input-number v-model="model.analysis" placeholder="请选择解析规则" style="width: 100%" /> -->
              <a-select v-model="model.analysis" style="width: 100%" placeholder="请选择解析规则">
                <a-select-option v-for="(l, i) in analysisOptions" :key="i" :value="l.value">
                  {{ l.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="助记码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zhujima">
              <a-input v-model="model.zhujima" placeholder="请输入助记码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="原产品编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入原产品编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="套件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taojian">
              <a-input-number v-model="model.taojian" placeholder="请输入套件" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="商品分组(DI码编码产品)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="productGroup"
            >
              <a-input-number
                v-model="model.productGroup"
                placeholder="请输入商品分组(DI码编码产品)"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="商品属性(内购、外购)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="productParam"
            >
              <a-input-number
                v-model="model.productParam"
                placeholder="请输入商品属性(内购、外购)"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="company">
              <a-input v-model="model.company" placeholder="请输入所属公司"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="创建人id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createid">
              <a-input-number v-model="model.createid" placeholder="请输入创建人id" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="修改人id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateid">
              <a-input-number v-model="model.updateid" placeholder="请输入修改人id" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { udiAnalysisList } from '@/api/udi'
export default {
  name: 'BmsMaterialForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        analysis: []
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/bms/bmsMaterial/add',
        edit: '/bms/bmsMaterial/edit',
        queryById: '/bms/bmsMaterial/queryById',
      },
      analysisOptions: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.getAnalysisOptions()
  },
  methods: {
    async getAnalysisOptions() {
      try {
        const params = { pageNo: 1, pageSize: 10000 }
        const r = await udiAnalysisList(params)
        // console.log('getAnalysisOptions', r)
        this.analysisOptions = r.result.records.map((e) => {
          return {
            label: e.label,
            value: e.label,
          }
        })
      } catch (_) {}
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      if (record.analysis) {
        record.analysis = record.analysis || []
      }
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>